package thread;

import thread.biz.*;
import thread.util.MixUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 模拟 支付订单 & 派送订单 对账处理
 */
public abstract class AbstractCheckBatch {
    // 需处理的订单id
    protected static List<Integer> ids = new ArrayList<>();
    static {
        for (int i = 1; i <= MixUtils.IDMAX; i++) {
            ids.add(i);
        }
    }
    // 支付订单服务，模拟延时 20ms
    protected POrderService pOrderService = new POrderService();
    // 派送订单服务，模拟延时 100ms
    protected DOrderService dOrderService = new DOrderService();

    // 处理 抽象方法
    abstract void doProcess() throws Exception;

    // 处理 方法
    public void process() throws Exception {
        final long start = System.currentTimeMillis();
        System.out.println("start ms is " + System.currentTimeMillis());
        doProcess();
        System.out.println("耗时 " + (System.currentTimeMillis() - start) + "ms");
        doFinal();
    }

    protected abstract void doFinal();

    // 模拟 对账判断 奇数订单id者存在差异
    protected Diff check(POrder pOrder, DOrder dOrder) {
        long id = pOrder.getId();
        if (id % 10 != 1)
            return null;
        return Diff.builder().pOrder(pOrder).dOrder(dOrder).diff("diff" + id).build();
    }

    // 模拟 差异入库，模拟延时 20-50ms
    protected void save(Diff diff) {
        MixUtils.sleepMs(2); // 模拟延时 2ms
        System.out.println("save diff: " + diff.toString());
    }
}
